package com.byt.consultation.fragment;

import android.os.Bundle;
import android.view.View;

import com.byt.base.fragment.BaseFragment;
import com.byt.common.adapter.lv.LvCommonAdapter;
import com.byt.common.adapter.lv.LvViewHolder;
import com.byt.common.bean.ConsultaBean;
import com.byt.common.view.OnNoDoubleClickListener;
import com.byt.common.view.refresh.RefreshHeaderView;
import com.byt.consultation.R;
import com.byt.consultation.activity.ConsultChatActivity;
import com.byt.consultation.bean.BageNum;
import com.byt.consultation.bean.MainBanner;
import com.byt.consultation.databinding.FragmentConsultMainBinding;
import com.byt.consultation.utils.AdapterDataUtils;
import com.byt.consultation.utils.MainUtil;
import com.byt.consultation.viewmodel.ConsultViewModel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 类名称：  HomeFragment
 * 类描述：  界面
 */
public class ConsultMainFragment extends BaseFragment<FragmentConsultMainBinding, ConsultViewModel>
        implements ConsultViewModel.IConsultUserView {
    private static ConsultMainFragment mfragment = null;
    private LvCommonAdapter<ConsultaBean> mLvCommonAdapter = null;
    private List<ConsultaBean> mConsultaBean = new ArrayList<>();

    public static ConsultMainFragment newsInstance() {
        mfragment = new ConsultMainFragment();
        return mfragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewModel.onBannerImg();
        viewModel.onBageNum();
        initRv();
    }

    private void initRv() {
        dataBinding.srlHomeLayout.setEnableLoadMore(false);
        dataBinding.srlHomeLayout.setEnableRefresh(true);
        dataBinding.srlHomeLayout.setRefreshHeader(new RefreshHeaderView(sContext).getHeaderStyleExpertWhite());
        dataBinding.srlHomeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.tryToRefresh();
            }
        });
        mLvCommonAdapter = new LvCommonAdapter<ConsultaBean>(sContext,mConsultaBean,R.layout.item_consulta_list_layout) {
            @Override
            protected void convert(LvViewHolder viewHolder, ConsultaBean consultaBean, int position) {
                AdapterDataUtils.loadConsultaData(viewHolder, consultaBean, position);
                viewHolder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(ConsultChatActivity.INP_CONSULTA_BEAN, consultaBean);
                        if (MainUtil.jumpActivity(sContext, ConsultChatActivity.class, bundle)) {
                            if (consultaBean.getRead_flag() == 0) {
                                consultaBean.setRead_flag(1);
                                mLvCommonAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        };
        dataBinding.nslvHomeConsultaList.setAdapter(mLvCommonAdapter);
    }

    @Override
    public void initFristData() {
        super.initFristData();
        setLoadSir(dataBinding.srlHomeLayout);
        showLoading();
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_consult_main;
    }

    @Override
    public ConsultViewModel getViewModel() {
        return new ConsultViewModel(4, 7);
    }

    @Override
    protected void onRetryBtnClick() {
        viewModel.tryToRefresh();
    }

    @Override
    protected String getFragmentTag() {
        return "ConsultMainFragment";
    }


    @Override
    public void onConsultLoaded(List<ConsultaBean> consultaBeans) {
        showContent();
        mConsultaBean.clear();
        mConsultaBean.addAll(consultaBeans);
        mLvCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void onBannerImg(List<MainBanner> banners) {

    }

    @Override
    public void onBageNum(BageNum bageNum) {

    }

}
